﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace MvcApplicationYoung.WcfStandaloneWinform
{
    public partial class WcfServiceWinForm : Form
    {
        DataTable dt;
        ServiceHost host = null;

        public WcfServiceWinForm()
        {
            InitializeComponent();
            dt = CreateGridTable();
            this.dataGridView1.DataSource=dt;
        }

        private DataTable CreateGridTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("名称");
            dt.Columns.Add("类型");
            dt.Columns.Add("参数");
            dt.Columns.Add("返回");
            return dt;
        }
  
        private void btnStart_Click(object sender, EventArgs e)
        {
            //http 绑定 
            Type serviceType = typeof(MvcApplicationYoung.WcfServiceLibrary.Service1);
            Uri httpAddress = new Uri("http://localhost:8888/YoungMVC/Service.svc/");
            host = new ServiceHost(serviceType, new Uri[] { httpAddress });
            WSHttpBinding basicHttpBinding = new WSHttpBinding();
            string address = "test";
            //http://localhost:8888/YoungMVC/Service.svc/test
            ServiceEndpoint endPoint=host.AddServiceEndpoint(typeof(MvcApplicationYoung.WcfServiceLibrary.IService1), basicHttpBinding, address);

            #if DEBUG
            foreach (OperationDescription od in endPoint.Contract.Operations)
               {

                   MvcApplicationYoung.WcfServiceLibrary.OwnBehavior1 bh = new MvcApplicationYoung.WcfServiceLibrary.OwnBehavior1();
                   bh.NInspector.evtBeforeCall += new WcfServiceLibrary.NewInspector.DelBeforeCall(BeforeCall);
                   od.Behaviors.Add(bh);
               }
            #endif
            
            //System.ServiceModel.Description.ServiceMetadataBehavior bh = new System.ServiceModel.Description.ServiceMetadataBehavior();
            //bh.HttpGetEnabled = true;
            //host.Description.Behaviors.Add(bh);

            ////net 绑定
            //Type serviceType = typeof(WcfServiceLibrary1.Service1);
            //Uri tcpAddress = new Uri("net.tcp://192.168.0.75:8000");
            //host = new ServiceHost(serviceType, new Uri[] { tcpAddress });
            //NetTcpBinding netTcpBinding = new NetTcpBinding();
            //netTcpBinding.Security.Mode = SecurityMode.None;
            //string address = "";
            //host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), netTcpBinding, address);
              //开始服务 
            host.Open();
            this.Text = "服务已启动";
        }

        private void BeforeCall(string operationName,object[] inputs)
        {
            DataRow newR=dt.NewRow();
            newR[0]=operationName;
            newR[1] ="BeforeCall";
            string inputString=string.Empty ;
            foreach (object ob in inputs)
            {
                inputString = inputString + ":" + ob.ToString() + "  ";
            }
            newR[2] = inputString;
            newR[3] = "";
            dt.Rows.Add(newR);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if ((host != null) && (host.State != CommunicationState.Closed))//判断服务是否关闭
            {
                host.Close();//关闭服务
                this.Text = "服务已关闭";
            }

        }

    }
}
